package flc.ast.fragment;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.cartoonavatarmaker.lib.core.AvatarColorBean;
import com.stark.cartoonavatarmaker.lib.core.AvatarColorType;
import com.stark.cartoonavatarmaker.lib.core.AvatarConst;
import com.stark.colorpicker.lib.ColorPicker;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.DressUpActivity;
import flc.ast.adapter.FormatColorAdapter;
import flc.ast.databinding.FragmentDressUpColorBinding;
import stark.common.basic.base.BaseNoModelFragment;

public class DressUpColorFragment extends BaseNoModelFragment<FragmentDressUpColorBinding> {

    private AvatarColorType mColorType;
    private FormatColorAdapter mAdapter;
    private ColorPicker mColorPicker;

    public static DressUpColorFragment newInstance(@NonNull AvatarColorType type) {
        DressUpColorFragment fragment = new DressUpColorFragment();
        fragment.mColorType = type;
        return fragment;
    }

    @Override
    protected int onCreate() {
        return R.layout.fragment_dress_up_color;
    }

    @Override
    protected void initView() {
        int spanCount = 3;
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 4);
        mDataBinding.rvContainer.setLayoutManager(layoutManager);
        FormatColorAdapter colorAdapter = new FormatColorAdapter(spanCount);
        mAdapter = colorAdapter;
        colorAdapter.setOnItemClickListener(this);
        colorAdapter.setNewInstance(getColorBeans(mColorType));
        mDataBinding.rvContainer.setAdapter(colorAdapter);
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        mAdapter.setSelPos(position);
        AvatarColorBean colorBean = mAdapter.getItem(position);
        if (colorBean.imgId == R.drawable.ic_cam_color_pick) {
            showColorPicker();
            return;
        }
        onSelColor(mAdapter.getItem(position).color);
    }

    private void onSelColor(int color) {
        if (getActivity() instanceof DressUpActivity) {
            ((DressUpActivity) getActivity()).setColor(mColorType, color);
        }
    }

    private List<AvatarColorBean> getColorBeans(AvatarColorType type) {
        List<AvatarColorBean> beans = new ArrayList<>();
        AvatarColorBean bean = new AvatarColorBean();
        bean.imgId = R.drawable.ic_cam_none;
        beans.add(bean);

        List<Integer> colors = AvatarConst.getColorsByType(type);
        for (Integer color : colors) {
            bean = new AvatarColorBean();
            bean.color = color;
            beans.add(bean);
        }

        bean = new AvatarColorBean();
        bean.imgId = R.drawable.ic_cam_color_pick;
        beans.add(bean);

        return beans;
    }

    public void showColorPicker() {
        if (mColorPicker == null) {
            mColorPicker = new ColorPicker(getContext());
            mColorPicker.setListener(pickedColor -> {
                onSelColor(pickedColor);
            });
        }
        mColorPicker.show();
    }
}